﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Meraz.Web.Models;
using Meraz.Core.Model;
using Meraz.Core.Managers;
using Meraz.Web.Filters;
using Ninject;
using Meraz.Core.Config;
namespace Meraz.Web.Controllers
{
    public class TwitterStudyController : Controller
    {
        [Inject]
        public TwitterManager TwitterManager { get; set; }

        [HttpPost]
        public ActionResult Add(string id)
        {
            //id is study name
            TwitterStudy study = TwitterManager.AddStudy(id);
            RowModel model = new RowModel();
            model.Values = new List<string>();
            model.Values.Add(study.ID.ToString());
            model.Values.Add(study.Name);
            model.Values.Add(study.Status);
            model.Values.Add(study.Start);
            model.Values.Add(study.End);
            model.Message = "Success";
            return Json(model);
        }

        [HttpPost]
        public ActionResult AddUser(string studyID, string username)
        {
            int id;
            RowModel model = new RowModel();
            if (int.TryParse(studyID, out id))
            {
                TwitterSection twitterSection =
                    (TwitterSection)ConfigHelper.GetSection("twitter");
                TwitterUser user = TwitterManager.AddUser(username, id, twitterSection);
                if (user != null)
                {
                    model.Values.Add(user.ID.ToString());
                    model.Values.Add(user.Name);
                    model.Values.Add(user.IsBlog.ToString().ToLower());
                    model.Values.Add(user.IsNewspaper.ToString().ToLower());
                    model.Values.Add(user.IsTopJournalist.ToString().ToLower());
                    model.Values.Add(user.IsTopPolitical.ToString().ToLower());
                    model.Message = "Success";
                }
            }
            if (string.IsNullOrEmpty(model.Message))
            {
                model.Message = "Fail";
            }
            return Json(model);
        }

        [HttpPost]
        public ActionResult RemoveUser(string studyID, string username)
        {
            int id;
            RowModel model = new RowModel();
            if (int.TryParse(studyID, out id))
            {
                if (TwitterManager.RemoveUser(username, id))
                {
                    model.Message = "Success";
                }
            }
            if (string.IsNullOrEmpty(model.Message))
            {
                model.Message = "Fail";
            }
            return Json(model);
        }

        [RolesAuthorize(Roles = UserRoles.Editor)]
        [HttpPost]
        public ActionResult Remove(string id)
        {
            RowModel model = new RowModel();
            int studyID;
            if (int.TryParse(id, out studyID)){
                bool result = TwitterManager.RemoveStudy(studyID);
                model.Message = (result)? "Success" : "Fail";
            }else
                model.Message = "Fail";
            return Json(model);
        }
        [ObjectFilter(Param = "study", RootType = typeof(TwitterStudy))]
        [HttpPost]
        public ActionResult Save(TwitterStudy study)
        {
            RowModel model = new RowModel();
            if (TwitterManager.Save(study))
            {
                model.Message = "Success";
                model.Values.Add(study.ID.ToString());
                model.Values.Add(study.Name);
                model.Values.Add(study.Status);
                model.Values.Add(study.Start);
                model.Values.Add(study.End);
            }
            else
            {
                model.Message = "Fail";
            }
            return Json(model);
        }
    }
}
